/**
 * mediator for configuration design and DesignNavigatorComp
 * */
package com.designCreator.view
{
	import com.designCreator.events.SwitchToDesignEvent;
	import com.designCreator.model.vo.DesignCreatorRequest;
	import com.designCreator.notifications.NotificationNames;
	import com.designCreator.view.components.ConfigurationDesign;
	
	import flash.events.Event;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.utils.ObjectUtil;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class ConfigurationDesignMediator extends Mediator implements IMediator
	{
		public static const STYLE_NAME:String = "styleConfigurationDesignMediator";
		public static const FABRIC_NAME:String = "fabricConfigurationDesignMediator";
		
		public static const MOVE_BACK:String = "moveBack";
		public static const MOVE_NEXT:String = "moveNext";
		
		
		public function ConfigurationDesignMediator(name:String,configurationDesign:ConfigurationDesign)
		{
			super(name,configurationDesign);
		}
		
		public function get configurationDesign():ConfigurationDesign
		{
			return viewComponent as ConfigurationDesign ;
		}
		
		//---------------------------------------------------------------
		//Overriden methods
		//---------------------------------------------------------------
		override public function onRegister():void
		{
			configurationDesign.addEventListener(MOVE_BACK,changeDesignConfig);
			configurationDesign.addEventListener(MOVE_NEXT,changeDesignConfig);
			/*configurationDesign.L1DesignHolder.addEventListener(SwitchToDesignEvent.SWITCH_TO_DESING_CONFIG,
				onSwitchToDesign,true);*/
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				NotificationNames.LOAD_FABRIC_L1_DATA_SUCCESS,
				NotificationNames.LOAD_FABRIC_L1_DATA_FAILURE,
				NotificationNames.LOAD_FABRIC_L2_DATA_FAILURE,
				NotificationNames.LOAD_FABRIC_L2_DATA_SUCCESS
			];
		}
		override public function handleNotification(note:INotification):void
		{
			switch ( note.getName() )
			{
				case NotificationNames.LOAD_FABRIC_L1_DATA_SUCCESS:
					//uncomment this to set l1design from the backend
					//setL1Designs(note.getBody() as XMLListCollection)
					loadL2Data();
					break;
				
				case NotificationNames.LOAD_FABRIC_L1_DATA_FAILURE:
					configurationDesign.L1DesignHolder.L1Designs = new XMLListCollection() ;
					break;
				
				case NotificationNames.LOAD_FABRIC_L2_DATA_FAILURE:
					//flexcaster.views.selectedIndex = PODCAST_VIEW;
					break;
				case NotificationNames.LOAD_FABRIC_L2_DATA_SUCCESS:
					//Alert.show('L2 data success:'+ObjectUtil.toString(note.getBody() as XMLListCollection));
					//configurationDesign.L1DesignHolder.L1Designs = note.getBody() as XMLListCollection ;
					break;
				
			}	
		}
		
		//---------------------------------------------------------------
		//Private methods
		//---------------------------------------------------------------
		private function changeDesignConfig(event:Event):void
		{
			if(event.type == MOVE_BACK)
			{
				onMove(configurationDesign.currentState,'BACK');
			}
			else
			{
				onMove(configurationDesign.currentState,'NEXT');
			}
			
		}	
		
		private function loadL2Data():void
		{
			var requestVO:DesignCreatorRequest = new DesignCreatorRequest();
			requestVO.L2DesignXmlPath = FlexGlobals.topLevelApplication.L2DesignXmlPath ;
			
			sendNotification(NotificationNames.GET_FABRIC_L2_DATA,requestVO);
		}
		
		private function onMove(oldState:String,dir:String):void
		{
			switch(dir)
			{
				case 'NEXT' :
					switch(oldState)
					{
						case "fabricDesignState":
							sendNotification(NotificationNames.LOAD_STYLE_TAB);
							break;
						case "sleeveState" :
							configurationDesign.currentState = "frontState";
							configurationDesign.designTitle = "Front Style";
							configurationDesign.designMedImg = "assets/shirts/23[2] (4).jpg";
							break;
						case "frontState" :
							configurationDesign.currentState = "backState";
							configurationDesign.designTitle = "Back Style";
							configurationDesign.designMedImg = "assets/shirts/23[4] (41).jpg";
							break;
						case "backState" :
							configurationDesign.currentState = "bottomState";
							configurationDesign.designTitle = "Bottom Style";
							configurationDesign.designMedImg = "assets/style/bottom/23[5] (3).jpg";
							break;
						case "bottomState" :
							configurationDesign.currentState = "collarState";
							configurationDesign.designTitle = "Collar Style";
							configurationDesign.designMedImg = "assets/style/collar/23[10].jpg";
							
							break;
						case "collarState" :
							configurationDesign.currentState = "pocketsState"
							configurationDesign.designTitle = "Pocket Style";
							configurationDesign.designMedImg = "assets/style/pockets/23CA0XDB0C.jpg";
							break;
						case "pocketsState" :
							configurationDesign.currentState = "cuffsState"
							configurationDesign.designTitle = "Cuff Style";
							configurationDesign.designMedImg = "assets/style/cuffs/23CAST45HT.jpg";
							break;
					}
					break;
				case 'BACK':
					switch(oldState)
					{
						
						case "sleeveState" :
							sendNotification(NotificationNames.LOAD_FABRIC_TAB);
							break;
						case "frontState" :
							configurationDesign.currentState = "sleeveState";
							configurationDesign.designTitle = "Sleeve";
							configurationDesign.designMedImg = "assets/shirts/23[2] (4).jpg";
							break;
						case "backState" :
							configurationDesign.currentState = "frontState";
							configurationDesign.designTitle = "Front Style";
							configurationDesign.designMedImg = "assets/shirts/23[2] (4).jpg";
							break;
						case "bottomState" :
							configurationDesign.currentState = "backState";
							configurationDesign.designTitle = "Back Style";
							configurationDesign.designMedImg = "assets/shirts/23[4] (41).jpg";
							break;
						case "collarState" :
							configurationDesign.currentState = "bottomState";
							configurationDesign.designTitle = "Bottom Style";
							configurationDesign.designMedImg = "assets/style/bottom/23[5] (3).jpg";
							break;
						case "pocketsState" :
							configurationDesign.currentState = "collarState";
							configurationDesign.designTitle = "Collar Style";
							configurationDesign.designMedImg = "assets/style/collar/23[10].jpg";
							break;
						case "cuffsState" :
							configurationDesign.currentState = "pocketsState"
							configurationDesign.designTitle = "Pocket Style";
							configurationDesign.designMedImg = "assets/style/pockets/23CA0XDB0C.jpg";
							break;
					}
					break;
				default:break;
			}
		}
		
		
		/**This method is used to set L1 designs for selected design in L1DesignHolder*/
		private function setL1Designs(dp:XMLListCollection):void
		{
			if(dp!=null)
			{
				configurationDesign.L1DesignHolder.L1Designs = dp ;
			}
			else
			{
				configurationDesign.L1DesignHolder.L1Designs = new XMLListCollection();
			}
		}
		
		/*private function onSwitchToDesign(event:SwitchToDesignEvent):void
		{
			
		}*/
			
	}
}